package com.wmzdq.photocenter.config;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.xml.transform.Source;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
//@enableWebMvc 千万不能加，加了会出问题
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
@ComponentScan(basePackages={"com.wmzdq.photocenter.controller"},useDefaultFilters=false,includeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,value=RestController.class)})
@ComponentScan(basePackages={"com.wmzdq.photocenter"},useDefaultFilters=false,includeFilters={@ComponentScan.Filter(type=FilterType.ANNOTATION,value={Component.class,ControllerAdvice.class,ControllerAdvice.class})})
public class WebMvcConfiguaration extends DelegatingWebMvcConfiguration  {

    
    
    //springmvc的国际化，可以不用，默认就是 AcceptHeaderLocaleResolver,具体看有道云笔记
    @Bean(name="localeResolver")
    public AcceptHeaderLocaleResolver localeResolver(){
        return new AcceptHeaderLocaleResolver();
    }
    
    //�?��要加！！不然自定义拦截器 无法生效
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        
        return super.requestMappingHandlerMapping();
    }
    

    
    @Bean(name="multipartResolver")  
    public CommonsMultipartResolver commonsMultipartResolver(){
        CommonsMultipartResolver upload=new CommonsMultipartResolver();
        upload.setDefaultEncoding("UTF-8");
        return upload;
        
    }
    
    @Bean
    public ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver=new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/jsp/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setOrder(0);
        return viewResolver;
    }
    
    //把viewResolver添加到容器中
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(viewResolver());
        
    }
    
    @Bean(name="validatorMessageSource")
    public ReloadableResourceBundleMessageSource validMessageSource(){
        ReloadableResourceBundleMessageSource valid=new ReloadableResourceBundleMessageSource();
        valid.setBasename("classpath:validator_messages");
        valid.setDefaultEncoding("UTF-8");
        return valid;
    }
    
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();
        List<MediaType> list=new ArrayList<MediaType>();
        list.add(MediaType.TEXT_HTML);
        list.add(MediaType.APPLICATION_JSON);
        
        converter.setSupportedMediaTypes(list);
        converter.setFeatures(SerializerFeature.QuoteFieldNames,SerializerFeature.WriteDateUseDateFormat);
        return converter;

    }

    @Override
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        
        Charset charset=Charset.forName("UTF-8");
        StringHttpMessageConverter converter=new StringHttpMessageConverter(charset);
        
        converters.add(fastJsonHttpMessageConverter());
        converters.add(converter);
        converters.add(new ResourceHttpMessageConverter());
        converters.add(new SourceHttpMessageConverter<Source>());
        converters.add(new AllEncompassingFormHttpMessageConverter());
        
    }
    
   @Bean
   public CommonsMultipartResolver multi(){
	   CommonsMultipartResolver mulit=new CommonsMultipartResolver();
	   mulit.setDefaultEncoding("UTF-8");
	   return mulit;
   }
}
